package tienda.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import tienda.modelo.Aviso;
import tienda.negocio.GestionarAviso;

/**
 * Servlet implementation class RegistraAvisoServlet
 */
@WebServlet("/RegistraAvisoServlet")
public class RegistraAvisoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegistraAvisoServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			
			// --- Obtengo los datos de los text		
						
			String titulo=request.getParameter("Txttitulo");
			String mensaje=request.getParameter("Txtmensaje");
			String fecinicio=request.getParameter("Txtfecinicio");
			String fecfin=request.getParameter("Txtfecfin");
			int ubicacionid=Integer.parseInt(request.getParameter("TxtUbicacion"));
			
			//int ubicacionid=1;
			int personaid=2;
									
			//RequestDispatcher rd = request.getRequestDispatcher("RegistroAviso.jsp");
			
					try {
						//---Registro Aviso
						
					Aviso Aviso;
					GestionarAviso regaviso= new GestionarAviso();
					Aviso=regaviso.insertar(titulo, mensaje, fecinicio, fecfin, ubicacionid, personaid);	
					request.setAttribute("Aviso",  Aviso);			
					
					System.out.println("Se registro con exito");
					
					//alert("Se registro con exito");
								
					
					} catch (Exception e) {
						request.setAttribute("error", e.getMessage());				
					}
						
					//rd.forward(request, response);
					
				} catch (Exception e) {
					System.out.println(e.getMessage());
				}
				
				RequestDispatcher rd = request
				.getRequestDispatcher("RegistrarAviso.jsp");
		rd.forward(request, response);
	}

}
